package com.example.manager.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.manager.common.QueryPageParam;
import com.example.manager.common.Result;
import com.example.manager.entity.Employees;
import com.example.manager.entity.Users;
import com.example.manager.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fwy
 * @since 2024-07-22
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    UsersService usersService;

    @PostMapping("login")
    public Result login(@RequestBody Users users) {
        List list = usersService.lambdaQuery()
                .eq(Users::getId, users.getId())
                .eq(Users::getPassword, users.getPassword()).list();
        return list.size() > 0 ? Result.suc(list.get(0)) : Result.fail();

    }

    //查询
    @GetMapping("/list")
    public List<Users> list() {
        return usersService.list();
    }

    //编辑
    @PostMapping("/mod")
    public Result mod(@RequestBody Users users) {
        boolean success = usersService.updateById(users);
        return success ? Result.suc(users) : Result.fail();
    }

    @PostMapping("/register")
    public Result register(@RequestBody Users users) {
        // 首先检查用户是否存在
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getId, users.getId());
        Users existingUser = usersService.getOne(queryWrapper, false);
        if (existingUser != null) {
            // 用户存在，检查密码是否为空
            if (existingUser.getPassword() == null || existingUser.getPassword().isEmpty()) {
                // 如果原密码为空，更新密码
                users.setPassword(users.getPassword());
                boolean success = usersService.updateById(users);
                return success ? Result.suc(users) : Result.fail();
            } else {
                // 密码不为空，不更新密码
                return Result.fail();
            }
        } else {

            return Result.fail();
        }
    }

    //删除
    @DeleteMapping("/del")
    public Result delete(@RequestParam String id) {
        return usersService.removeById(id) ? Result.suc() : Result.fail();
    }

    @PostMapping("/listPage1")
    public Result listPage1(@RequestBody QueryPageParam query) {
        System.out.println(query);
        HashMap param = query.getParam();
        String id = (String) param.get("id");

        Page<Users> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Users> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(id)) {
            lambdaQueryWrapper.eq(Users::getId, id); // 确保使用了正确的字段名称
        }
        IPage result = usersService.pageCC(page, lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }


    //获取单个数据
    @PostMapping("/listPage")
    public Result listPage(@RequestBody String id) {
        LambdaQueryWrapper<Users> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(id)) {
            lambdaQueryWrapper.eq(Users::getId, id); // 确保使用了正确的字段名称
        }
        Users user = usersService.getOne(lambdaQueryWrapper, false);
        if (user != null) {
            return Result.suc(user); // 假设Result有一个suc方法来设置数据
        } else {
            return Result.fail(); // 假设Result有一个fail方法来设置错误信息
        }
    }

    @PostMapping("/newLogin")
    public Result newLogin(@RequestBody Users users) {
        // 首先检查用户是否存在
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getId, users.getId());
        Users existingUser = usersService.getOne(queryWrapper, false);

        if (existingUser != null) {
            // 用户存在，不执行任何操作，直接返回成功响应
            return Result.fail();
        } else {

            users.setPassword(null); // 确保数据库允许密码列为NULL，或者设置默认密码
            boolean success = usersService.save(users);
            return success ? Result.suc(users) : Result.fail();
        }
    }
    @PostMapping("/changePassword")
    public Result changePassword( @RequestBody Users users) {
        // 检查用户是否存在
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getId, users.getId());
        Users existingUser = usersService.getOne(queryWrapper, false);

        if (existingUser != null) {
            // 用户存在，更新密码
            existingUser.setPassword(users.getPassword()); // 使用 Users 对象的 setPassword 方法
            boolean success = usersService.updateById(existingUser);
            return success ? Result.suc() : Result.fail();
        } else {
            // 用户不存在
            return Result.fail();
        }
    }
}


